package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.AplCar;
import com.cs157a.dao.RdiPart;
import com.cs157a.util.UiUtil;

import java.util.List;

/**
 * This class shows the dialog for input new part.
 * 
 * @author hhong
 * 
 */
public class NewAutoModelDialog extends JDialog {

	private static final long serialVersionUID = 20012100301L;
	private static final int FIELD_WIDTH = 20;
	private AposApplicationController controller;
	private JButton saveButton;
	private JButton cancelButton;

	private JLabel labelMaker;
	private JComboBox comboBoxMaker;
	private JLabel labelModel;
	private JTextField textFieldModel;
	private JLabel labelYear;
	private JTextField textFieldYear;
	private JLabel labelDesc;
	private JTextField textFieldDesc;
	private JLabel labelLit;
	private JTextField textFieldLit;
	private JLabel labelEng;
	private JTextField textFieldEng;
	private JLabel labelCubic;
	private JTextField textFieldCubic;
	private JLabel labelRlink;
	private JComboBox comboBoxRLink;
	private PartNumberAssignmentPanel assignmentPanel;

	public NewAutoModelDialog(final AposApplicationController controller) {
		super();
		this.controller = controller;

		setPreferredSize(new Dimension(550, 500));

		saveButton = new JButton(CommonString.SAVE);
		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (textFieldModel.getText().isEmpty()
						|| textFieldModel.getText().length() > 25
						|| textFieldYear.getText().isEmpty()
						|| textFieldYear.getText().length() > 4
						|| textFieldDesc.getText().isEmpty()
						|| textFieldDesc.getText().length() > 51
						|| textFieldLit.getText().isEmpty()
						|| textFieldLit.getText().length() > 4
						|| textFieldEng.getText().isEmpty()
						|| textFieldEng.getText().length() > 3
						|| textFieldCubic.getText().isEmpty()
						|| textFieldCubic.getText().length() > 6)
					JOptionPane
							.showMessageDialog(
									rootPane,
									"Please Enter All the field! "
											+ "\n  Year with Maximum 6 digits"
											+ "\n  Liter with Maximum Length of 4,"
											+ "\n  Engine type with Maximum Length of 3,"
											+ "\n  Cubic Inches with Maximum Length of 6!");
				else {
					AplCar autoModel = getAutoModel();
					controller.saveAutoModel(autoModel, comboBoxMaker
							.getSelectedItem().toString(), assignmentPanel
							.getRadcrx());
					// dispose();
					setVisible(false);
					clearAllFields();
				}
			}
		});
		cancelButton = new JButton(CommonString.CANCEL);
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				clearAllFields();
			}
		});
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		assignmentPanel = new PartNumberAssignmentPanel(controller);

		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.add(createInnerPanel(), BorderLayout.NORTH);
		contentPanel.add(assignmentPanel, BorderLayout.CENTER);

		add(contentPanel, BorderLayout.NORTH);
		add(buttonPanel, BorderLayout.SOUTH);
		setModalityType(ModalityType.APPLICATION_MODAL);

	}

	protected AplCar getAutoModel() {
		AplCar car = new AplCar();
		try {
			car.setAll(
					textFieldModel.getText(),
					textFieldYear.getText(),
					textFieldDesc.getText(),
					textFieldLit.getText(),
					textFieldEng.getText(),
					textFieldCubic.getText(),
					Integer.parseInt(comboBoxRLink.getSelectedItem().toString()
							.trim()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return car;
	}

	protected void clearAllFields() {
		// TODO Auto-generated method stub
		textFieldModel.setText("");
		textFieldYear.setText("");
		textFieldDesc.setText("");
		textFieldLit.setText("");
		textFieldEng.setText("");
		textFieldCubic.setText("");

	}

	private JPanel createInnerPanel() {
		labelMaker = new JLabel(AutoColumn.MAKER.getName());
		List<String> list = controller.getAllMakeNames();
		comboBoxMaker = new JComboBox(list.toArray());
		labelModel = new JLabel(AutoColumn.MODEL.getName());
		textFieldModel = new JTextField(FIELD_WIDTH);

		labelYear = new JLabel(AutoColumn.YEAR.getName());
		textFieldYear = new JTextField(FIELD_WIDTH);

		labelDesc = new JLabel(AutoColumn.DESCRIPTION.getName());
		textFieldDesc = new JTextField(FIELD_WIDTH);

		labelLit = new JLabel(AutoColumn.LITRES.getName());
		textFieldLit = new JTextField(FIELD_WIDTH);

		labelEng = new JLabel(AutoColumn.ENGINE.getName());
		textFieldEng = new JTextField(FIELD_WIDTH);

		labelCubic = new JLabel(AutoColumn.CUBIC.getName());
		textFieldCubic = new JTextField(FIELD_WIDTH);

		labelRlink = new JLabel(AutoColumn.RLINK.getName());
		comboBoxRLink = new JComboBox(controller.getAllRlink().toArray());

		JPanel innerPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.HORIZONTAL;
		int x = 0;
		int y = 0;
		innerPanel.add(labelMaker, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelModel, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelYear, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelDesc, c);
		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelLit, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelEng, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(labelCubic, c);

//		c.gridx = x;
//		c.gridy = ++y;
//		innerPanel.add(labelRlink, c);
		//
		x = 1;
		y = 0;
		c.gridx = x;
		c.gridy = y;
		innerPanel.add(comboBoxMaker, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldModel, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldYear, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldDesc, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldLit, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldEng, c);

		c.gridx = x;
		c.gridy = ++y;
		innerPanel.add(textFieldCubic, c);

//		c.gridx = x;
//		c.gridy = ++y;
//		innerPanel.add(comboBoxRLink, c);

		return innerPanel;
	}
}
